package com.tanhua.manage.listener;

import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 动态审核监听类
 */
@Component
@RocketMQMessageListener(
        topic = "tanhua-publish",consumerGroup = "mytanhuapublish"
)
@Slf4j
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;
    
    /**
     * 动态审核
     * @param publishId
     */
    @Override
    public void onMessage(String publishId) {
        log.debug("*******************动态审核开始了************动态id:{}*******",publishId);
        //a.获取消息队列中动态发布id
        //b.根据动态发布id查询发布表 获取发布对象（得到动态文本 与 动态图片urls）
        Publish publish = publishApi.querySinglePublish(publishId);
        Integer state=2;// 状态0：待审核，1：已审核，2：已驳回
        if(publish != null) {
            String txt = publish.getTextContent();//文本
            List<String> medias = publish.getMedias();//图片urls
            //c.调用华为云组件 审核文本 与 图片
            boolean flag1 = huaWeiUGCTemplate.textContentCheck(txt);
            //将list集合转为string数组
            boolean flag2 = huaWeiUGCTemplate.imageContentCheck(medias.toArray(new String[]{}));
            if(flag1 && flag2){
                state = 1;
            }
            //d.得到审核结果，更新动态发布表将state修改为1 or 2
            publishApi.updatePublish(publishId,state);
            log.debug("*******************动态审核结束了*************发布id:{}****状态:{}**",publishId,state);
        }
    }
}
